﻿package lz.controls
{
	
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	/**
	 * CheckBox
	 * @author lxh
	 */	
	public class CheckBox extends BaseButton
	{
		/**
		 *改变事件类型 
		 */		
		public static const CHANGE:String="change";
		/**
		 * 文字显示
		 */		
		private var __labelTxt:TextField;
		/**
		 * 选择状态mc
		 */		
		private var __selectedMc:Sprite;
		/**
		 * 默认状态
		 */		
		private var __skin:MovieClip;
		/**
		 *标签字符 
		 */		
		private var _label:String;
		/**
		 *位置说明 
		 */		
		private var _labelPlacement:String;
		/**
		 * 是否不需要标签文字
		 */		
		private var _noLabel:Boolean;
		/**
		 *间隔x 
		 */		
		private var grapX:Number=4;
		/**
		 *间隔y 
		 */		
		private var grapY:Number=4;
		/**
		 *皮肤类描述 
		 */		
		private var _skinClass :String;
		
		public function CheckBox()
		{
			super();
		}
		/**
		 *配置UI 
		 * 
		 */		
		protected override function configUI():void
		{
			skin = "SkinCheckBox";
			__skin.gotoAndStop("normal");
			autoRepeat=false;
			selected=false;
			label="label";
			labelPlacement="right";
			__labelTxt.autoSize=labelPlacement;
			__labelTxt.wordWrap=true;
			addListener();
			drawLayout();
		}
		/**
		 *添加事件 
		 * 
		 */		
		private function addListener():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER,mouseHandle);
			this.addEventListener(MouseEvent.ROLL_OUT,mouseHandle);
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseHandle);
			this.addEventListener(MouseEvent.CLICK,mouseHandle);
			function mouseHandle(e:MouseEvent):void
			{
				switch(e.type)
				{
					case MouseEvent.ROLL_OVER:
						__skin.gotoAndStop("rollover");
						break;
					case MouseEvent.ROLL_OUT:
						__skin.gotoAndStop("normal");
						break;
					case MouseEvent.MOUSE_DOWN:
						__skin.gotoAndStop("down");
						break;
					case MouseEvent.CLICK:
						selected = !selected;
						dispatchEvent(new Event("select"));
						__skin.gotoAndStop("rollover");
				}
			}
		}
		/**
		 * 绘制背景
		 */		
		override protected function drawBackground():void
		{
			
		}
		/**
		 * 绘制布局
		 * 
		 */		
		override protected function drawLayout():void
		{
			__skin.x=0;
			__skin.y=0;
			__selectedMc.x=0;
			__selectedMc.y=0;
			__labelTxt.width = this.width - __skin.width;
			if(_labelPlacement=="center")
			{
				__labelTxt.x=grapX;
				__labelTxt.width=__skin.width-2*grapX
				__labelTxt.autoSize="center";	
			}
			else if(_labelPlacement=="left")
			{
				
				__labelTxt.autoSize="right";
				__labelTxt.x=-__labelTxt.width-grapX;
			}
			else if(_labelPlacement=="right")
			{
				__labelTxt.x=__skin.width+grapX;
				__labelTxt.autoSize="left";
			}
			__skin.y=(__labelTxt.height-__skin.height)>>1
			__selectedMc.y=(__labelTxt.height-__skin.height)>>1;
		}
		/**
		 *设置皮肤 
		 * @param value
		 * 
		 */		
		public function set skin(value:String):void
		{
			DisplayObjectUtil.removeAllChildren(this);
			_skinClass = value;
			_symbolSkin = Reflection.createInstance(_skinClass);
			this.addChild(_symbolSkin);
			
			__labelTxt		= _symbolSkin.__labelTxt;
			__selectedMc	= _symbolSkin.__selectedMc;
			__skin			= _symbolSkin.__skin;
		}
		/**
		 *设置标签 
		 * @param value
		 * 
		 */		
		public function set label(value:String):void
		{
			if(value==null)
			{
				_label="";
			}
			else
			{
				_label=value;
			}
			__labelTxt.htmlText=_label;
			drawLayout();
		}
		/**
		 *获取标签 
		 * @return 
		 * 
		 */		
		public function get label():String
		{
			return _label;
		}
		/**
		 *设置nolabel状态 
		 * @param value
		 * 
		 */		
		public function set noLabel(value:Boolean):void
		{
			_noLabel=value;
			if(_noLabel==true)
			{
				if(contains(__labelTxt))removeChild(__labelTxt)
			}
			else
			{
				if(!contains(__labelTxt))addChild(__labelTxt)
			}
		}
		/**
		 *获取 nolabel状态 
		 * @return 
		 * 
		 */		
		public function get noLabel():Boolean
		{
			return _noLabel;
		}
		/**
		 *设置label位置描述 
		 * @param value
		 * 
		 */		
		public function set labelPlacement(value:String):void
		{
			_labelPlacement=value;
			drawLayout();
		}
		/**
		 *获取label位置描述  
		 * @return 
		 * 
		 */		
		public function get labelPlacement():String
		{
			return _labelPlacement;
		}
		/**
		 * 设置选择状态
		 * @param value
		 * 
		 */		
		override public function set selected(value:Boolean):void
		{
			var ischange:Boolean=false;
			if(_selected!=value)
			{
				ischange=true;
			}
			_selected=value;
			if(_selected)
			{
				__selectedMc.visible=true;
			}
			else
			{
				__selectedMc.visible=false;
			}
			if(ischange==true)
			{
				dispatchEvent(new Event(CheckBox.CHANGE));
			}
		}
		/**
		 *获取选择状态 
		 * @return 
		 * 
		 */		
		override public function get selected():Boolean
		{
			return _selected;
		}
		/**
		 *获取内容宽度 
		 * @return 
		 * 
		 */		
		public function get contentWidth():Number
		{
			return __skin.width+grapX+__labelTxt.width;
		}
		/**
		 *获取内容高度 
		 * @return 
		 * 
		 */		
		public function get contentHeight():Number
		{
			return __labelTxt.height;
		}
	}
}